package com.example.springboot.service.impl;

import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.Result;
import com.example.springboot.pojo.User;
import com.example.springboot.service.UserService;
import com.example.springboot.service.ValidationService;
import com.example.springboot.util.Encrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ValidationService validationService;

    @Override
    public User findByRoomNumber(String roomNumber) {
        return userMapper.findByRoomNumber(roomNumber);
    }

    @Override
    public PageBean<User> getAllUser(Integer pageNum,Integer pageSize) {
        //创建pageBean对象
        PageBean<User> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<User> users = userMapper.getAllUser();
        Page<User> p = (Page<User>) users;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void addUser(String roomNumber, String name, String id, String phoneNumber, String password,String email) {
        //密码加密
        String encryptPsd = Encrypt.toMD5(password);
        userMapper.addUser(roomNumber,name,id,phoneNumber,encryptPsd,email);
    }

    @Override
    public void updateUser(String roomNumber, String name, String id, String phoneNumber,String email) {
        userMapper.updateUser(roomNumber,name,id,phoneNumber,email);
    }

    @Override
    public void updatePassword(String email, String password) {
        userMapper.updatePassword(email,password);
    }

    @Override
    public void deleteUser(String roomNumber){
        userMapper.deleteUser(roomNumber);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }
}
